import Taro from "@tarojs/taro";

export const openConfirm = (): Promise<boolean> => {
  return new Promise((resolve) => {
    Taro.showModal({
      content: "您没有打开麦克风和摄像头的权限，是否去设置打开？",
      confirmText: "确认",
      cancelText: "取消",
      success: (res) => {
        if (res.confirm) {
          console.log("用户点击确认");
          Taro.openSetting({
            success: (res) => {
              console.log("手动开启摄像头录音功能：", res.authSetting);
              let authorizeMic = res.authSetting["scope.record"];
              let authorizeCamera = res.authSetting["scope.camera"];
              if (authorizeMic && authorizeCamera) {
                resolve(true);
              } else {
                resolve(false);
              }
            },
          });
        } else if (res.cancel) {
          console.log("用户点击取消");
          Taro.showToast({
            title: "拒绝授权,无法进入视频！",
            icon: "none",
            duration: 2000,
          });
        }
      },
    });
  });
};

export const checkDeviceAuthorize = (): Promise<boolean> => {
  return new Promise((resolve, reject) => {
    if (!Taro.getSetting || !Taro.getSetting()) {
      // 微信测试版 获取授权API异常，目前只能即使没授权也可以通过
      resolve(false);
    }
    Taro.getSetting().then((result) => {
      let authorizeMic = result.authSetting["scope.record"];
      let authorizeCamera = result.authSetting["scope.camera"];
      if (
        result.authSetting["scope.camera"] &&
        result.authSetting["scope.record"]
      ) {
        // 授权成功
        resolve(true);
      } else {
        // 没有授权，弹出授权窗口
        // 注意： wx.authorize 只有首次调用会弹框，之后调用只返回结果，如果没有授权需要自行弹框提示处理
        console.log("getSetting 没有授权录音/摄像头，弹出授权窗口", result);
        Taro.authorize({
          scope: "scope.record",
        })
          .then((res) => {
            console.log("授权录音", res);
            authorizeMic = true;
            if (authorizeCamera) {
              resolve(true);
            }
          })
          .catch((error) => {
            console.log("拒绝录音", error);
            authorizeMic = false;
          });
        Taro.authorize({
          scope: "scope.camera",
        })
          .then((res) => {
            console.log("授权摄像头", res);
            authorizeCamera = true;
            if (authorizeMic) {
              resolve(true);
            } else {
              openConfirm().then((conformRes) => {
                console.log("openConfirm-conformRes::=====", conformRes);
                if (conformRes) {
                  resolve(true);
                } else {
                  reject(new Error("authorize fail"));
                }
              });
            }
          })
          .catch((error) => {
            console.log("拒绝开启摄像头", error);
            authorizeCamera = false;
            openConfirm().then((conformRes) => {
              if (conformRes) {
                resolve(true);
              } else {
                reject(new Error("authorize fail"));
              }
            });
          });
      }
    });
  });
};

export const checkDeviceVoice = (): Promise<boolean> => {
  return new Promise((resolve) => {
    Taro.getSetting().then((result) => {
      if (result.authSetting["scope.record"]) {
        resolve(true);
      } else {
        Taro.authorize({
          scope: "scope.record",
        })
          .then((res) => {
            console.log("授权录音", res);
            resolve(true);
          })
          .catch((error) => {
            console.log("拒绝开启录音", error);
            Taro.showModal({
              content: "您没有打开麦克风的权限，是否去设置打开？",
              confirmText: "确认",
              cancelText: "取消",
              success: (res) => {
                if (res.confirm) {
                  Taro.openSetting({
                    success: (res) => {
                      console.log("手动开启录音功能：", res.authSetting);
                      let authorizeMic = res.authSetting["scope.record"];
                      if (authorizeMic) {
                        resolve(true);
                      } else {
                        resolve(false);
                      }
                    },
                  });
                } else if (res.cancel) {
                  Taro.showToast({
                    title: "拒绝授权,无法开始录音",
                    icon: "none",
                    duration: 2000,
                  });
                }
              },
            });
          });
      }
    });
  });
};

//定位检查
export const checkLocationPermission = (): Promise<boolean> => {
  return new Promise((resolve) => {
    Taro.getSetting().then((result) => {
      let locationPer = result.authSetting["scope.userLocation"];
      if (locationPer) {
        resolve(true);
      } else {
        Taro.authorize({
          scope: "scope.userLocation",
        })
          .then(() => {
            resolve(true);
          })
          .catch(() => {
            resolve(false);
          });
      }
    });
  });
};
